﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KeyboardDebounce
{
    public partial class MainForm : Form
    {
        private NotifyIcon trayIcon;
        private ContextMenu trayMenu;

        public MainForm()
        {
            InitializeComponent();

            this.Text = "键盘误触防护程序";
            this.Width = 300;
            this.Height = 120;

            Label label = new Label()
            {
                Text = "当前状态：运行中（<100ms重复键已屏蔽）",
                AutoSize = true,
                Top = 20,
                Left = 10
            };
            this.Controls.Add(label);

            Button btnExit = new Button()
            {
                Text = "退出",
                Left = 180,
                Top = 50,
                Width = 80
            };
            btnExit.Click += (s, e) => Application.Exit();
            this.Controls.Add(btnExit);

            // 托盘菜单
            trayMenu = new ContextMenu();
            trayMenu.MenuItems.Add("退出", (s, e) => Application.Exit());

            trayIcon = new NotifyIcon()
            {
                Text = "键盘防抖正在运行",
                Icon = SystemIcons.Application,
                ContextMenu = trayMenu,
                Visible = true
            };

            this.Resize += (s, e) =>
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.Hide();
                    trayIcon.ShowBalloonTip(1000, "键盘防抖程序", "程序已最小化到托盘", ToolTipIcon.Info);
                }
            };

            trayIcon.DoubleClick += (s, e) =>
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
            };


            CheckBox autoStartCheckBox = new CheckBox()
            {
                Text = "开机自动启动",
                Left = 10,
                Top = 50,
                Checked = AutoStartHelper.IsAutoStartEnabled()
            };
            autoStartCheckBox.CheckedChanged += (s, e) =>
            {
                if (autoStartCheckBox.Checked)
                {
                    AutoStartHelper.EnableAutoStart();
                }
                else
                {
                    AutoStartHelper.DisableAutoStart();
                }
            };
            this.Controls.Add(autoStartCheckBox);
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            trayIcon.Dispose();
            base.OnFormClosing(e);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // MainForm
            // 
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Name = "MainForm";
            this.Load += new System.EventHandler(this.MainForm_Load);
            this.ResumeLayout(false);

        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }
    }
}
